package org.jobtask.config;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public final class ObjectFactory {

	public static Object getInstance(String className){
		Object instance=null;
		try {
			instance=Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return instance;
	}
	
	public static Object getInstance(String className,Object... params){
		Object instance=null;
		Class[] typeArray=new Class[params.length];
		for(int index=0;index<typeArray.length;index++){
			typeArray[index]=params[index].getClass();
		}
		try {
			Class clazz=Class.forName(className);
			Constructor<String> construct=clazz.getConstructor(typeArray);
			instance=construct.newInstance(params);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return instance;
	}
}
